package com.xkdx.guangguang.card;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.xkdx.guangguang.module.network.AbsModule;

public class CardInfoModule extends AbsModule {
	public List<CardInfo> cardInfoList;
	public List<Bank> banklist;

	@Override
	public void parseData() throws Exception {
		backJson = (JSONArray) result;
		try {
			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("getCardListByCondition")) {
					parseCardInfo(jsonObj.getJSONObject("DetailInfo"));
				} else if (sortOfAct.equals("getBankList")) {
					parseBankList(jsonObj.getJSONObject("DetailInfo"));
				}

			}
		} catch (Exception e) {
			throw e;
		}
	}

	private void parseBankList(JSONObject jsonObject) throws Exception {
		banklist = new ArrayList<Bank>();
		try {
			JSONArray arry = jsonObject.getJSONArray("DetailInfo");
			if (arry.length() != 0) {
				Bank bank = new Bank();
				bank.setID("");
				bank.setBankName("全部优惠");
				banklist.add(bank);
			}

			for (int i = 0; i < arry.length(); i++) {
				JSONObject opt = (JSONObject) arry.opt(i);
				Bank bank = new Bank();
				bank.setID(opt.getString("ID"));
				bank.setBankName(opt.getString("BankName"));
				bank.setLogo(opt.getString("Logo"));
				banklist.add(bank);
			}

		} catch (JSONException e) {
			throw e;
		}

	}

	private void parseCardInfo(JSONObject jsonObject) throws Exception {
		cardInfoList = new ArrayList<CardInfo>();
		try {
			JSONArray arry = jsonObject.getJSONArray("DetailInfo");
			for (int i = 0; i < arry.length(); i++) {
				JSONObject opt = (JSONObject) arry.opt(i);
				CardInfo cardInfo = new CardInfo();
				cardInfo.setBankName(opt.getString("BankName"));
				cardInfo.setCinfoID(opt.getString("CinfoID"));
				cardInfo.setDiscount(opt.getString("Discount"));
				cardInfo.setShopName(opt.getString("ShopName"));
				cardInfo.setShopType(opt.getString("ShopType"));
				cardInfo.setDistance(opt.getString("Distance"));
				cardInfo.setLogo(opt.getString("Logo"));
				cardInfoList.add(cardInfo);
			}
		} catch (Exception e) {
			throw e;
		}

	}

}
